<?php

namespace Api\Controller;

use Think\Controller;

class ShopController extends CommonController
{
    private $eatName = array();

    public function index()
    {
        if (isset($_GET['pageNum'])) {
            $page = I('pageNum');
            $page = (int)$page * 10;
        } else {
            $page = 0;
        }

        $map = array();
        if(I('stype') == 'juli'){
            //按距离查询
            $lat = I('la');
            $lng = I('lo');
            $where=" WHERE wid=".session('wid');
            $where .= " and (status =1 or status = 2) and is_shield=0 and join_end_time >'".date('Y-m-d H:i:s', time())."'";

            $sql="SELECT *, ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($lat*PI()/180-latitude*PI()/180)/2),2)+COS($lat*PI()/180)*COS(latitude*PI()/180)*POW(SIN(($lng*PI()/180-longitude*PI()/180)/2),2)))*1000) AS juli
     FROM tp_shop".$where ." ORDER BY is_top desc,juli ASC,last_time desc,is_dao asc";
            $select_sql =$sql." LIMIT 0,".$page;
            $list = M()->query($select_sql);
            //筛选美食
            $eatArr = $this->eatCates();
            foreach ($list as $key => $value) {
                if ($value['juli'] < 1000) {
                    $list[$key]['juli'] = $value['juli'].'米';
                } else {
                    $distance = $value['juli'] / 1000;
                    $list[$key]['r_distance'] = round($distance, 2).'公里';
                }

                if (in_array($value['cate'] , $eatArr)) {
                    $list[$key]['is_eat'] = 1;
                } else {
                    $list[$key]['is_eat'] = 0;
                }
                // is_pin is_kan is_qiang 活动项目
                $goods = M('goods')->where(['shop_id' => $value['shop_id']])->select();
                foreach ($goods as $kk => $vv) {
                    if ($vv['is_pin']) {
                        $list[$key]['has_pin'] = 1;
                    }
                    if ($vv['is_kan']) {
                        $list[$key]['has_kan'] = 1;
                    }
                    if ($vv['is_qiang']) {
                        $list[$key]['has_qiang'] = 1;
                    }
                }
            }
        }else{
            // 需要判断分类是一级分类还是二级分类
            if(intval(I('cid'))>0) {
                //$map['cid'] = $_GET['cid'];
                $cid = I('cid');
                $cate = M("Cate")->where("id=$cid")->find();

                //$map['join_end_time'] = array('<=',date('Y-m-d H:i:s',time()));
                if ($cate['pid']) {
                    $map['cid'] = I('cid');
                } else {
                    // 是一级分类的时候
                    $map['_string'] = "cid in (select id from tp_cate where pid = $cid)";
                    //$map['cid'] = 0;
                }
            }
            if(I('aid') !=""){
                $where['id'] = I('aid');
                if($where['id'] != 0){
                    //不是选择全辖区
                    $city = M('city')->where($where)->find();
                }else{
                    //选择全辖区
                    $map2['wid'] = session('wid');
                    $wechat = M("Wechat")->where($map2)->find();
                    $where2['city_code'] = $wechat['city'];
                    $city = M("City")->where($where2)->find();
                }
                $map['county_name | site_name'] = ['like',"%".$city['city_name']."%"];
            }

            if ($_GET['type']) {
                $map['type'] = $_GET['type'];
            }
            if(I('stype')){
                switch(I('stype')){
                    case 'no':
                        break;
                    case 'junjia';
                        $order = ',person_money desc';
                        break;
                    case 'hot';
                        $order = ',hot_num desc';
                        break;
                }
            }else{
                $order = '';
            }
            $map['wid'] = session('wid');
            $map['status'] = [['eq',1],['eq',2],'or'];
            $map['is_shield'] = 0;//屏蔽

            $list = M("shop")->where($map)
                ->where(['join_end_time' => ['gt', date('Y-m-d H:i:s', time())]])
                ->order("is_top desc,is_dao asc,last_time desc".$order)
                ->limit(0, $page)
                ->select();
            //筛选美食
            $eatArr = $this->eatCates();
            foreach ($list as $key => $value) {
                if (in_array($value['cate'] , $eatArr)) {
                    $list[$key]['is_eat'] = 1;
                } else {
                    $list[$key]['is_eat'] = 0;
                }
                // is_pin is_kan is_qiang 活动项目
                $goods = M('goods')->where(['shop_id' => $value['shop_id']])->select();
                foreach ($goods as $kk => $vv) {
                    if ($vv['is_pin']) {
                        $list[$key]['has_pin'] = 1;
                    }
                    if ($vv['is_kan']) {
                        $list[$key]['has_kan'] = 1;
                    }
                    if ($vv['is_qiang']) {
                        $list[$key]['has_qiang'] = 1;
                    }
                }
            }
            foreach ($list as $key => $value) {
                $list[$key]['type'] = trim($value['type']);
                //商户距离计算
                if (I('la') != 'undefined' && I('lo') != 'undefined') {
                    if ($value['latitude'] && $value['longitude']) {
                        $earthRadius = 6367000;
                        $lat1 = (float) I('la');//纬度
                        $lng1 = (float) I('lo');//经度
                        $lat2 = (float) $value['latitude'];
                        $lng2 = (float) $value['longitude'];
                        $lat1=($lat1* pi()) / 180;
                        $lng1=($lng1* pi()) / 180;
                        $lat2=($lat2* pi()) / 180;
                        $lng2=($lng2* pi()) / 180;
                        $calcLongitude = $lng2-$lng1;
                        $calcLatitude = $lat2-$lat1;
                        $stepOne = pow(sin($calcLatitude/2),2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude/2),2);
                        $stepTwo = 2* asin(min(1, sqrt($stepOne)));
                        $distance = round($earthRadius*$stepTwo);
                        if ($distance < 1000) {
                            $list[$key]['r_distance'] = $distance.'米';
                        } else {
                            $distance = $distance / 1000;
                            $list[$key]['r_distance'] = round($distance, 2).'公里';
                        }
                    } else {
                        $list[$key]['r_distance'] = '';
                    }
                } else {
                    $list[$key]['r_distance'] = '';
                }
            }
        }

        //$this->ajaxReturn($list);
        
        foreach ($list as $k => $v) {
            $eq['shop_id'] = $v['shop_id'];
            $eq['is_pin'] = 1;
            $eq['status'] = 1;
            $list[$k]['has_pin'] = M("Goods")->where($eq)->find();

            $eq1['shop_id'] = $v['shop_id'];
            $eq1['is_pin'] = 0;
            $eq1['is_kan'] = 1;
            $eq1['status'] = 1;
            $list[$k]['has_kan'] = M("Goods")->where($eq1)->find();

            $eq2['shop_id'] = $v['shop_id'];
            $eq2['is_qiang'] = 1;
            $eq2['status'] = 1;
            $list[$k]['has_qiang'] = M("Goods")->where($eq2)->find();

            $eq3['shop_id'] = $v['shop_id'];
            $eq3['status'] = 1;
            // $eq3['is_qiang'] = 1;
            $list[$k]['has_coupon'] = M("Coupon")->where($eq3)->find();
            //免预约
            $eq4['shop_id'] = $v['shop_id'];
            $eq4['yuyue'] = 1;
            $info = M("shop_card_yh")->where($eq4)->find();
            $list[$k]['has_mian'] = ($info['yuyue']==1?1:0);
            //同城折扣
            $eq5['shop_id'] = $v['shop_id'];
            $eq5['discount'] = ['gt',0];
            $list[$k]['has_zhe'] = M("shop_card_yh")->where($eq5)->find()?1:0;
            //同城特惠信息
            $eq6['shop_id'] = $v['shop_id'];
            $info3 = M("shop_card_yh")->where($eq6)->find();
            $list[$k]['discount_remark'] = $info3['discount_remark'];
            $list[$k]['discount'] = $info3['discount'];
        }
        $this->ajaxReturn($list);
    }

    public function follow()
    {
        $model = M('ShopFollow');

        $eq['shop_id'] = $_POST['shop_id'];
        $eq['openid'] = $_POST['openid'];
        if ($model->where($eq)->find()) {
            if ($model->where($eq)->delete()) {
                $arr = array("err_code" => 0, "err_msg" => "-1");
                $this->ajaxReturn($arr);
            } else {
                $arr = array("err_code" => 400, "err_msg" => "网络忙！");
                $this->ajaxReturn($arr);
            }
        } else {
            $eq['create_time'] = date("Y-m-d H:i:s");
            if ($model->add($eq)) {
                $arr = array("err_code" => 0, "err_msg" => "1");
                $this->ajaxReturn($arr);
            } else {
                $arr = array("err_code" => 400, "err_msg" => "网络忙！");
                $this->ajaxReturn($arr);
            }
        }
    }

    public function topic()
    {
        $map = array();
        $map['cid'] = $_GET['cid'];
        $map['wid'] = session('wid');
        // $map['status'] = 1;
        $list = M("Topic")->where($map)->order("last_time desc")->select();
        $this->ajaxReturn($list);
    }
    //开通同城卡（店铺）
    public function  openShopcard()
    {
        $shop_id = I('shop_id');
        $value = I();
        $info = M('shop_card_yh')->where(['shop_id'=>$shop_id])->find();
        if($info['status' == 0]){
            $arr = array("err_code" => 400, "err_msg" => "不要重复申请！");
            $this->ajaxReturn($arr);
        }
        if(!I('num')){
            $arr = array("err_code" => 400, "err_msg" => "请选择特权日！");
            $this->ajaxReturn($arr);
        }

        if(I('num') == 1){
            $data['is_day'] = 1;
            if(!strpos(I('day'),',') && !strpos(I('day'),'，') && count(I('day'))>2){
                $arr = array("err_code" => 400, "err_msg" => "特权日格式错误！");
                $this->ajaxReturn($arr);
            }else{
                $days = str_replace('，',',',I('days'));
                $data['days'] = $days;
            }
        }
        if(I('num') == 2){
            $data['is_week'] = 1;
            $data['week_0'] = (strpos($value['day'],'week_0') === false) ? 0 : 1;
            $data['week_1'] = (strpos($value['day'],'week_1') === false) ? 0 : 1;
            $data['week_2'] = (strpos($value['day'],'week_2') === false) ? 0 : 1;
            $data['week_3'] = (strpos($value['day'],'week_3') === false) ? 0 : 1;
            $data['week_4'] = (strpos($value['day'],'week_4') === false) ? 0 : 1;
            $data['week_5'] = (strpos($value['day'],'week_5') === false) ? 0 : 1;
            $data['week_6'] = (strpos($value['day'],'week_6') === false) ? 0 : 1;
        }
        if(I('shuoming')){
            $data['remark'] = I('shuoming');
        }
        I('yuyue') == 'true' ? $data['yuyue'] = 1 : $data['yuyue'] = 0;
        $data['shop_id'] = $shop_id;
        if(floatval(I('discount'))>0){
            if(floatval(I('discount'))>10){
                $arr = array("err_code" => 400, "err_msg" => "优惠折扣超出范围！");
                $this->ajaxReturn($arr);
            }
            $data['discount'] = floatval(I('discount'));
        }else{
            $arr = array("err_code" => 400, "err_msg" => "请填写优惠折扣！");
            $this->ajaxReturn($arr);
        }
        $data['discount_remark'] = I('discount_remark');
        $data['count'] = I('count');
        $data['frequency'] = I('frequency');
        $data['wid'] = session('wid');
        $data['create_time'] = date('Y-m-d H:i:s',time());
        if ($_POST['num'] == 1) {
            $data['is_day'] = 1;
            $data['is_week'] = 0;
        } else {
            $data['is_day'] = 0;
            $data['is_week'] = 1;
            unset($data['days']);
        }
        if ($info['status'] == 1) {
            $data['status'] = 1;
            $a = M('shop_card_yh')->where(['shop_id' => $shop_id, 'wid' => session('wid')])->save($data);
        } else {
            $data['status'] = 0;
            $a = M('shop_card_yh')->add($data);
        }
        if ($a) {
            $arr = array("err_code" => 0, "err_msg" => "操作成功");
            $this->ajaxReturn($arr);
        } else {
            $arr = array("err_code" => 400, "err_msg" => "网络忙！");
            $this->ajaxReturn($arr);
        }
    }
    //获取店铺同城卡卡信息
    public function getCardInfo()
    {
        $shop_id = I('shop_id');
        $info = M('shop_card_yh')->where(['shop_id'=>$shop_id])->find();
        $this->ajaxReturn($info);
    }

    public function haveShop()
    {
        $map['wid'] = session('wid');
        $map['openid'] = $_POST['openid'];
        $map['is_pay'] = 1;
        $shop_arr = M("Shop")->where($map)->find();
        if ($shop_arr) {
            if ($shop_arr['status']) {
                if ($shop_arr['status'] == 1) {
                    $arr = array("is_have" => true, "title" => '商户中心', "url" => "/pages/mine/shop/shop", 'imgUrl' => 'https://www.58zltc.com/Public/images/wx/shopCenter.png');
                    //$arr = array("is_have" => true, "title" => '店铺管理', "url" => "/pages/mine/shop/edit", imgUrl => 'https://www.58zltc.com/Public/images/wx/zhuangXiu.png');
                }
            } else {
                $arr = array("is_have" => false, "title" => '店铺已申请', "url" => "", 'imgUrl' => 'https://www.58zltc.com/Public/images/wx/shopCenter.png');
            }
        } else {
            //是不是店员
            $shopkeeper_openid = M('member')->where(['wid' => session('wid'), 'openid' => $_POST['openid']])->getField('shopkeeper_openid');
            $maps['wid'] = session('wid');
            $maps['openid'] = $shopkeeper_openid;
            $maps['is_pay'] = 1;
            $keeper_shop = M("Shop")->where($maps)->find();
            if ($keeper_shop) {
                if ($keeper_shop['status']) {
                    if ($keeper_shop['status'] == 1) {
                        $arr = array("is_have" => true, "title" => '商户中心', "url" => "/pages/mine/shop/shop?is_clerk=1&openid=".$shopkeeper_openid, 'imgUrl' => 'https://www.58zltc.com/Public/images/wx/shopCenter.png');
                    }
                } else {
                    $arr = array("is_have" => false, "title" => '店铺已申请', "url" => "", 'imgUrl' => 'https://www.58zltc.com/Public/images/wx/shopCenter.png');
                }
            } else {
                //是否认领过商铺
                $shop_arr_r = M("renling")->where($map)->find();
                $shop_find = M("Shop")->where(['shop_id' => $shop_arr_r['shop_id']])->find();
                if ((!empty($shop_find)) && ($shop_find['status'] == 2)) {
                    $arr = array("is_have" => false, "title" => '店铺已认领', "url" => "", 'imgUrl' => 'https://www.58zltc.com/Public/images/wx/shopCenter.png');
                } else {
                    $arr = array("is_have" => false, "title" => '我要开店', "url" => "/pages/shop/add", 'imgUrl' => 'https://www.58zltc.com/Public/images/wx/shopCenter.png');
                }
            }
        }
        $this->ajaxReturn($arr);
    }
//    public function haveShop()
//    {
//        $map['wid'] = session('wid');
//        $map['openid'] = $_POST['openid'];
//        $map['is_pay'] = 1;
//        $shop_arr = M("Shop")->where($map)->find();
//        if ($shop_arr) {
//            if ($shop_arr['status']) {
//                if ($shop_arr['status'] == 1) {
//                    $arr = array("is_have" => true, "title" => '商户中心', "url" => "/pages/mine/shop/shop", imgUrl => 'https://www.58zltc.com/Public/images/wx/shopCenter.png');
//                    //$arr = array("is_have" => true, "title" => '店铺管理', "url" => "/pages/mine/shop/edit", imgUrl => 'https://www.58zltc.com/Public/images/wx/zhuangXiu.png');
//                }
//            } else {
//                $arr = array("is_have" => false, "title" => '店铺已申请', "url" => "", imgUrl => 'https://www.58zltc.com/Public/images/wx/shopCenter.png');
//            }
//        } else {
//            //是否认领过商铺
//            $shop_arr_r = M("renling")->where($map)->find();
//            $shop_find = M("Shop")->where(['shop_id' => $shop_arr_r['shop_id']])->find();
//            if ((!empty($shop_find)) && ($shop_find['status'] == 2)) {
//                $arr = array("is_have" => false, "title" => '店铺已认领', "url" => "", imgUrl => 'https://www.58zltc.com/Public/images/wx/shopCenter.png');
//            } else {
//                $arr = array("is_have" => false, "title" => '我要开店', "url" => "/pages/shop/add", imgUrl => 'https://www.58zltc.com/Public/images/wx/shopCenter.png');
//            }
//        }
//        $this->ajaxReturn($arr);
//    }

    public function item()
    {
        //$map['wid'] = session('wid');
        $map['shop_id'] = $_GET['shop_id'];
        $info = M("Shop")->where($map)->find();
        $info['pics'] = explode(",", $info['pics']);
        $info['shop_banner'] = explode(",", $info['shop_banner']);

        $eq['shop_id'] = I("shop_id");
        $eq['openid'] = I("openid");
        $info['is_follow'] = M("ShopFollow")->where($eq)->getField('id');
        //店铺评价
        $info['pinlun'] = M("shop_evaluate")->alias('a')
                                            ->field('a.*, b.headimgurl, b.nickname')
                                            ->join('tp_member b on a.openid= b.openid')
                                            ->where(['a.shop_id' => I("shop_id")])
                                            ->order('a.last_time desc')
                                            ->limit('0, 20')
                                            ->select();
        //店铺同城卡
        $info['card'] = M('shop_card_yh')->where(['shop_id'=>I("shop_id"),'status'=>1])->find();
        $info['open_card'] = M('card_log')->where(['wid'=>session('wid'),'openid'=>I('openid'),'end_time' => ['GT', date('Y-m-d H:i:s', time())],'is_pay'=>1])->find()?1:0;
        if ($info['pinlun']) {
            foreach ($info['pinlun'] as $k => $v) {
                $str = $info['pinlun'][$k]['pics'];
                $info['pinlun'][$k]['pics'] = explode(',', $str);
            }
        }
        $this->ajaxReturn($info);
    }

    public function renling()
    {
        $eq['wid'] = session('wid');
        $eq['openid'] = $_POST['openid'];
        $eq['is_pay'] = 1;
        $_POST['wid'] = session('wid');
        foreach ($_POST as $key => $value) {
            if (!is_numeric($value) && empty($value)) {
                $arr = array("err_code" => 400, "err_msg" => "请填写全部信息！");
                $this->ajaxReturn($arr);
            }
        }
        if (M("Shop")->where($eq)->find()) {
            $arr = array("err_code" => 400, "err_msg" => "您已入驻本平台！");
            $this->ajaxReturn($arr);
        }
        $fee = M('join')->where(['title'=>I('join_fee')])->find();

        //存到表里
        $d['wid'] = session('wid');
        $d['openid'] = $_POST['openid'];
        $d['shop_id'] = $_POST['shop_id'];
        $d['username'] = $_POST['username'];
        $d['tel'] = $_POST['tel'];
        $d['status'] = 0;

        $data['wid'] = session('wid');
        $data['shop_id'] = $_POST['shop_id'];
        $data['openid'] = $_POST['openid'];
        $data['out_trade_no'] = $this->build_order_no();
        $data['title'] = "商家认领";
        $data['remark'] = '认领';
        $data['price'] = $fee['price'];
        $data['pay_money'] = $fee['price'];
        $data['join_days'] = $fee['days'];
        $data['status'] = 0;
        $data['is_pay'] = 0;
        $data['create_time'] = date("Y-m-d H:i:s");
        if(!$log_id = M("ShopOrder")->add($data)){
            $arr = array("err_code"=>400,"err_msg"=>"网络忙，请稍后再试！");
            $this->ajaxReturn($arr);
        }

        if (M("Renling")->add($d)) {
            $map['wid'] = session('wid');
            $wechat = M("Wechat")->where($map)->find();
            $appid = $wechat['appid'];
            $mch_id = $wechat['mch_id'];
            $key = $wechat['partner_key'];

            $url = "https://".$_SERVER['HTTP_HOST'].U("Notify/renLing",
                    array('out_trade_no'=>$data['out_trade_no'],'latitude'=>I('latitude'),'longitude'=>I('longitude')));
            vendor('Weixinpay.WxPayJsApiPay');
            $tools = new \JsApiPay();
            $input = new \WxPayUnifiedOrder();
            $input->SetBody("智联同城");
            $input->SetAttach("商家认领");
            $input->SetOut_trade_no($data['out_trade_no']);
            $input->SetTotal_fee(($fee['price']*100));
            $input->SetTime_start(date("YmdHis"));
            $input->SetTime_expire(date("YmdHis", time() + 600));
            $input->SetNotify_url($url);   //支付回调地址，这里改成你自己的回调地址。
            $input->SetTrade_type("JSAPI");
            $input->SetOpenid($_POST['openid']);
            $input->SetAppid($appid);
            $input->SetMch_id($mch_id);

            try{
                $wxorder = \WxPayApi::unifiedOrder($input, $key);
            }catch(Exception $e){
                $arr = array("err_code"=>400,"err_msg"=>$e->getMessage().$fee['price']);
                $this->ajaxReturn($arr);
            }
            if($wxorder['return_code'] == "FAIL"){
                $arr = array("err_code"=>400,"err_msg"=>$wxorder['return_msg'].$fee['price']);
                $this->ajaxReturn($arr);
            }
            try{
                $jsApiParameters = $tools->GetJsApiParameters($wxorder, $key);
            }catch(Exception $e){
                $arr = array("err_code"=>400,"err_msg"=>$e->getMessage());
                $this->ajaxReturn($arr);
            }
            echo $jsApiParameters;

//
//            $arr = array("err_code" => 0, "err_msg" => "申请提交成功！");
//            $this->ajaxReturn($arr);
        } else {
            $arr = array("err_code" => 400, "err_msg" => "网络忙，请稍后再试！");
            $this->ajaxReturn($arr);
        }
    }

    public function add()
    {
        // $arr = array("err_code"=>400, "err_msg"=>"平台升级中，暂停入驻！");
        // $this->ajaxReturn($arr);

        $eq['wid'] = session('wid');
        $eq['openid'] = $_POST['openid'];
        $eq['is_pay'] = 1;
        $find_shop = M("Shop")->where($eq)->find();
        if ($find_shop) {
            if ($find_shop['status']) {
                $arr = array("err_code" => 400, "err_msg" => "您已入驻本平台！");
            } else {
                $arr = array("err_code" => 400, "err_msg" => "您申请的店铺正在审核中..");
            }
            $this->ajaxReturn($arr);
        }
        foreach ($_POST as $key => $value) {
            if (!is_numeric($value) && empty($value)) {
                $arr = array("err_code" => 400, "err_msg" => "请填写全部信息！");
                $this->ajaxReturn($arr);
            }
        }
        if ($_POST['sub_cate'] != '全部') {
            $_POST['cate'] = $_POST['sub_cate'];
        }
        $where['title'] = $_POST['cate'];
        $where['wid'] = session('wid');
        $where['type'] = 'shop';
        $_POST['cid'] = M("Cate")->where($where)->getField("id");
        // $days = M('Join')->where(array('id'=>$_POST['join_id']))->getField('days');
        // $_POST['join_end_time'] = date('Y-m-d H:i:s',strtotime('+'.$days.' day'));
        $_POST['wid'] = session('wid');
        $_POST['create_time'] = date("Y-m-d H:i:s");
        $_POST['status'] = 3;
        //入驻的时长
        $join_arr = M('join')->where(['title' => $_POST['join_fee'], 'wid' => session('wid')])->find();
        $days_num = $join_arr['days'];
        $_POST['join_fee_time'] = $days_num * 60 * 60 * 24;

        if ($shop_id = M("Shop")->add($_POST)) {
            $this->getShopOrder($shop_id);
            $map['shop_id'] = $shop_id;
            $data['is_pay'] = 1;
            M("Shop")->where($map)->save($data);
            $arr = array("err_code" => 0, "err_msg" => "发布成功！");
            $this->ajaxReturn($arr);
        } else {
            $arr = array("err_code" => 400, "err_msg" => "网络忙，请稍后再试！");
            $this->ajaxReturn($arr);
        }

    }

    public function getAddForm()
    {
        $map['wid'] = session('wid');
        $wechat = M("Wechat")->where($map)->find();

        $map['id'] = $_GET['id'];

        $top_input = array('type' => 'top');

        $where['city_code'] = $wechat['city'];
        $city = M("City")->where($where)->find();
        $where1['pid'] = $city['id'];
        $site_arr2 = M("City")->where($where1)->getField("city_name", true);
        if ($wechat['city'] == 0) {
            $site_arr1 = array("全国");
        } else {
            $site_arr1 = array($city["city_name"]);
            if (!$site_arr2) {
                $site_arr2 = array("全辖区");
            }
        }
        $site_input = array(
            'type' => 'picker2',
            'title' => '所在区域',
            'name' => array('county_name', 'site_name'),
            'range' => array($site_arr1, $site_arr2),
            'value' => array(0, 0)
        );

        $cate_map['wid'] = session('wid');
        $cate_map['type'] = 'shop';
        $cate_map['pid'] = 0;

        $cates = M("Cate")->where($cate_map)->field("id as cid,title")->select();
        // dump($cates);
        foreach ($cates as $k => $v) {
            $cate_map['pid'] = $v['cid'];
            $cate_range[] = $v['title'];
            $sub_cates = M("Cate")->where($cate_map)->field("id as cid,title")->select();
            if ($sub_cates) {
                $cates[$k]['sub_cates'] = $sub_cates;
                foreach ($sub_cates as $key => $value) {
                    $cates[$k]['sub_range'][] = $value['title'];
                }
            } else {
                $cates[$k]['sub_cates'][] = array('cid' => 0, 'title' => '全部');
                $cates[$k]['sub_range'][] = '全部';
            }
        }
        $cate_input = array(
            'type' => 'picker3',
            'title' => '店铺分类',
            'cates' => $cates,
            'name' => array('cate', 'sub_cate'),
            'range' => array($cate_range, $cates[0]['sub_range']),
            'value' => array(0, 0),
        );
        $res['add_url'] = "https://www.58zltc.com/api.php/Shop/add";
        //$res['add_url'] = "https://58zltc.bbrfx.cn/api.php/Shop/add";
        $res['index_url'] = "/pages/shop/shop";

        $form[] = array('type' => 'text', 'title' => '店铺名称', 'name' => 'name');
        $form[] = $cate_input;
        $form[] = $site_input;
        $form[] = array('type' => 'text', 'title' => '店铺地址', 'name' => 'address');
        $form[] = array('type' => 'number', 'title' => '人均消费', 'name' => 'person_money');
        $form[] = array('type' => 'time2', 'title' => '营业时间', 'name1' => 'start_time', 'value1' => '09:00', 'name2' => 'end_time', 'value2' => '21:00');
        $form[] = array('type' => 'top');
        $form[] = array('type' => 'pic', 'title' => '店铺Logo', 'name' => 'logo_url', 'num' => 1, 'pics' => array());
        $form[] = array('type' => 'top');
        $form[] = array('type' => 'pic', 'title' => '店铺图片', 'name' => 'pics', 'num' => 2, 'pics' => array());
        $form[] = array('type' => 'top');
        $form[] = array('type' => 'textarea', 'title' => '详细介绍', 'name' => 'content');
        $form[] = array('type' => 'top');
        $form[] = array('type' => 'text', 'title' => '联系人', 'name' => 'username');
        $form[] = array('type' => 'text', 'title' => '联系电话', 'name' => 'tel');
        $form[] = array('type' => 'top');
        // $form[] = array('type'=>'price', 'title'=>'平台入驻费', 'price'=>$wechat['shop_fee'], 'unit'=>'年');

        $eq['wid'] = session('wid');
        // $top_arr = array("免费体验");
        $top_arr = M("Join")->where($eq)->getField('title',true);
        $id_arr = M("Join")->where($eq)->getField('id',true);
        // if($top_arr2){
        //     $top_arr = array_merge($top_arr,$top_arr2);
        // }
        $form[] = array('type' => 'picker1', 'title' => '平台入驻费', 'name' => 'join_fee'
        , 'range' => $top_arr
        , 'id'=> $id_arr
        , 'value' => 0);
        $res['form'] = $form;
        $this->ajaxReturn($res);
    }

    public function getRenlingForm()
    {
        $map['wid'] = session('wid');
        $wechat = M("Wechat")->where($map)->find();

        $map['id'] = $_GET['id'];

        $top_input = array('type' => 'top');

        $form[] = array('type' => 'text', 'title' => '联系人', 'name' => 'username');
        $form[] = array('type' => 'text', 'title' => '联系电话', 'name' => 'tel');
        $form[] = array('type' => 'top');
        // $form[] = array('type'=>'price', 'title'=>'平台入驻费', 'price'=>$wechat['shop_fee'], 'unit'=>'年');
        $res['add_url'] = "https://www.58zltc.com/api.php/Shop/renling";
        $res['index_url'] = "/pages/shop/shop";
         $eq['wid'] = session('wid');
        // // $top_arr = array("免费体验");
         $top_arr = M("Join")->where($eq)->getField('title',true);
        // // if($top_arr2){
        // //     $top_arr = array_merge($top_arr,$top_arr2);
        // // }
         $form[] = array('type'=>'picker1', 'title'=>'平台入驻费', 'name'=>'join_fee'
                 ,'range'=>$top_arr
                 , 'value'=>0);
        $res['form'] = $form;
        $this->ajaxReturn($res);
    }

    //
    public function shopInfo () {
        $shopId = I('shop_id');
        $shopInfo = M('shop')->where(['shop_id' => $shopId])->find();
        $this->ajaxReturn($shopInfo);
    }

    //
    public function getJoinInfo () {

        $joinInfo = M('join')->where(['wid' => session('wid')])->select();
        $this->ajaxReturn($joinInfo);
    }
    public function renew (){
        $wid= session('wid');
        $join_info = M('Join')->where(array('id'=>$_POST['join_id']))->find();
        $shop_info = M('Shop')->where(array('shop_id'=>$_POST['shop_id']))->find();
        if(empty($join_info) || empty($shop_info)){
            $arr = array("err_code" => 400, "err_msg" => "参数错误！");
            $this->ajaxReturn($arr);
        }
        $this->renewShopOrder($_POST['shop_id']);
    }

    //
    public function eatCates () {
        $wid= session('wid');
        $this->eatName[] = '美食';
        $eat = M('cate')->where(['title' => '美食', 'pid' => '0', 'wid' => $wid])->find();
        $this->recursion($eat['id']);
        //$this->ajaxReturn($this->eatName);
        return $this->eatName;
    }
    public function recursion ($id) {
        $list = M('cate')->where(['pid' => $id])->select();
        if ($list) {
            foreach ($list as $key => $value) {
                $this->eatName[] = $value['title'];
                if ($value['id']) {
                    $this->recursion($value['id']);
                }
            }
        }
    }

    public function delShopBanner () {
        $id = $_GET['shop_id'];
        $path = $_GET['path'];
        $shop = M('shop')->field('shop_banner')->where(['shop_id' => $id])->find();
        $shopArr = explode(',', $shop['shop_banner']);
        foreach ($shopArr as $k => $v) {
            if ($v == $path) {
                unset($shopArr[$k]);
                $shopStr = implode(',', $shopArr);
                $suc = M('shop')->where(['shop_id' => $id])->save(['shop_banner' => $shopStr]);
                $suc ? $this->ajaxReturn(1) : $this->ajaxReturn(0);
            }
        }
        $this->ajaxReturn(1);
    }

    public function delShopJiaoYuImg () {
        $openid = $_GET['openid'];
        $path = $_GET['path'];
        $delType = $_GET['type'];
        $shop = M('shop')->where(['openid' => $openid, 'wid' => session('wid')])->find();
        $JYList = M('jiaoyu_news')->where(['shop_id' => $shop['shop_id'], 'type' => $delType])->find();
        $shopArr = explode(',', $JYList['pics']);
        foreach ($shopArr as $k => $v) {
            if ($v == $path) {
                unset($shopArr[$k]);
                $shopStr = implode(',', $shopArr);
                $suc = M('jiaoyu_news')->where(['shop_id' => $shop['shop_id'], 'type' => $delType])->save(['pics' => $shopStr]);
                $suc ? $this->ajaxReturn(1) : $this->ajaxReturn(0);
            }
        }
        $this->ajaxReturn(1);
    }

    public function delShopJiaoYuMem () {
        $openid = $_GET['openid'];
        $path = $_GET['path'];
        $delType = $_GET['type'];
        $num = $_GET['num'];
        if ($num == 1) {
            //删除头像图片
            $shop = M('shop')->where(['openid' => $openid, 'wid' => session('wid')])->find();
            $JYList = M('jiaoyu_member')->where(['shop_id' => $shop['shop_id'], 'type' => $delType])->save(['headimg' => '']);
            $JYList ? $this->ajaxReturn(1) : $this->ajaxReturn(1);
        } elseif ($num == 2) {
            //删除介绍图片
            $shop = M('shop')->where(['openid' => $openid, 'wid' => session('wid')])->find();
            $JYList = M('jiaoyu_member')->where(['shop_id' => $shop['shop_id'], 'type' => $delType])->find();
            $shopArr = explode(',', $JYList['pics']);
            foreach ($shopArr as $k => $v) {
                if ($v == $path) {
                    unset($shopArr[$k]);
                    $shopStr = implode(',', $shopArr);
                    $suc = M('jiaoyu_member')->where(['shop_id' => $shop['shop_id'], 'type' => $delType])->save(['pics' => $shopStr]);
                    $suc ? $this->ajaxReturn(1) : $this->ajaxReturn(0);
                }
            }
        }
        $this->ajaxReturn(1);
    }

    public function delShopPicture () {
        $where = [];
        if ($_GET['shop_id']) {
            $id = $_GET['shop_id'];
            $where['shop_id'] = $id;
        }
        if ($_GET['shop_openid']) {
            $openid = $_GET['shop_openid'];
            $where['openid'] = $openid;
            $where['wid'] = session('wid');
        }
        $path = $_GET['path'];
        $shop = M('shop')->field('pics')->where($where)->find();
        $shopArr = explode(',', $shop['pics']);
        foreach ($shopArr as $k => $v) {
            if ($v == $path) {
                unset($shopArr[$k]);
                $shopStr = implode(',', $shopArr);
                $suc = M('shop')->where($where)->save(['pics' => $shopStr]);
                $suc ? $this->ajaxReturn(1) : $this->ajaxReturn(0);
            }
        }
        $this->ajaxReturn(1);
    }

    public function delShopLogo () {
        $openid = $_GET['shop_openid'];

        $suc = M('shop')->where(['openid' => $openid, 'wid' => session('wid')])->save(['logo_url' => '']);
        $suc ? $this->ajaxReturn(1) : $this->ajaxReturn(0);
    }

    public function addYuyue () {
        $post = $_POST;
        $post['wid'] = session('wid');
        $post['state'] = 1;
        $post['create_time'] = date('Y-m-d H:i:s', time());
        if (!$post['username']) {
            $this->ajaxReturn(['error' => 1, 'msg' => '用户名不能为空']);
        }
        if (!$post['phone']) {
            $this->ajaxReturn(['error' => 1, 'msg' => '手机号不能为空']);
        }
        if(!preg_match("/^1[345789]{1}\d{9}$/",$post['phone']) || !is_numeric($post['phone'])){
            $this->ajaxReturn(['error' => 1, 'msg' => '手机号格式错误']);
        }
        //是否预约过
      /*  $where = [
                    'shop_id' => $post['shop_id'],
                    'openid'  => $post['openid'],
                    'wid'     => $post['wid'],
                    'state'  =>1
                 ];
        $row = M('shop_yuyue')->where($where)->find();
        if ($row) {
            $this->ajaxReturn(['error' => 1, 'msg' => '您已预约过了']);
        }*/
        $suc = M('shop_yuyue')->add($post);
        if ($suc) {
            $this->ajaxReturn(['error' => 0, 'msg' => '预约成功']);
        } else {
            $this->ajaxReturn(['error' => 1, 'msg' => '网络繁忙']);
        }
    }

    public function getYuyue () {
        $openid = I('openid');
        $where['a.wid'] = session('wid');
        $where['a.openid'] = $openid;
        $list = M('shop_yuyue')
              ->alias('a')
              ->field('a.create_time as ctime, a.state as astate, a.remark as aremark, b.*')
              ->join('tp_shop b on a.shop_id = b.shop_id')
              ->where($where)
              ->order('a.last_time desc')
              ->select();
        $this->ajaxReturn($list);
    }

    public function getShopYuyue () {
        $shopid = I('shop_id');
        $where['a.shop_id'] = $shopid;
        $where['a.wid'] = session('wid');
        $list = M('shop_yuyue')->alias('a')
                               ->field('a.*, b.headimgurl, b.nickname')
                               ->join('tp_member b on a.openid = b.openid')
                               ->where($where)
                               ->order('a.last_time desc')
                               ->select();
        $this->ajaxReturn($list);
    }

    public function confirmYuyue () {
        $id = I('id');
        $row = M('shop_yuyue')->where(['id' => $id])->save(['state' => 2]);
        if ($row) {
            //预约成功模板消息
            sendYuyueMessage($id);
            $this->ajaxReturn(['error' => 0, 'msg' => '预约已确认']);
        } else {
            $this->ajaxReturn(['error' => 1, 'msg' => '网路繁忙']);
        }
    }

    public function addPingjia () {
        $post = $_POST;
        $post['wid'] = session('wid');
        $post['create_time'] = date('Y-m-d H:i:s', time());
        $suc = M('shop_evaluate')->add($post);
        if ($suc) {
            $this->ajaxReturn(['error' => 0, 'msg' => '评价成功']);
        } else {
            $this->ajaxReturn(['error' => 1, 'msg' => '网络繁忙']);
        }
    }
    public function getShopAgreement () {
        $type = I('type');
        $arr = M('agreement')->where(['type' =>$type , 'state' => 1, 'wid' => session('wid')])->find();
        $this->ajaxReturn($arr);
    }
    public function getShopAgreementF () {
        $arr = M('agreement')->where(['type' => 1, 'state' => 1, 'wid' => session('wid')])->find();
        $this->ajaxReturn($arr);
    }

    public function getTCCPrice () {
        $list = [];
        $where['wid'] = session('wid');
       /* //价格
        $listP = M('card_fee')->where($where)->select();*/
        //同城卡图片
        $pic = M('card_pics')->where(['wid'=>session('wid'),'status'=>1])->order('status desc')->find();
        //协议
        $listX = M('agreement')->where($where)->where(['type' => 2, 'state' => 1])->find();
        //同城卡
        $listT = M('card')->where($where)->where(['used' => 1])->order('days asc')->select();
        //我开通的卡
        $listM = M('card_log')->alias('l')
            ->join('tp_card c on c.id = l.cid')
            ->where(['l.is_pay'=>1,'l.wid'=>session('wid')])
            ->order('l.end_time desc')
            ->find();

        $list['pic'] = $pic;
        $list['mycard'] = $listM;
        $list['card'] = $listT;
        $list['agree'] = $listX;
        $this->ajaxReturn($list);
    }
    //开卡信息
    public function haveTCCard () {
        $return = [];
        $openid = I('openid');
        $list = M('card_log')->where(['wid' => session('wid'), 'openid' => $openid, 'end_time' => ['GT', date('Y-m-d H:i:s', time())], 'is_pay' => 1])->order('id desc')->select();
        $list ? $a = 1 : $a = 0;
        $return['have_car'] = $a;
        $return['info'] = $list[0];
        $return['wid'] = session('wid');
        $this->ajaxReturn($return);
    }

    public function memberday(){
        $shop_id = $_POST['id'];
        $map = array();
        $map['wid'] = session('wid');
        $map['shop_id'] = $shop_id;
        $info = M('shop_card_yh')->where($map)->find();
        if(empty($info)){
            $item = array(
                array('name'=>'week_0','value'=>'星期日'),
                array('name'=>'week_1','value'=>'星期一'),
                array('name'=>'week_2','value'=>'星期二'),
                array('name'=>'week_3','value'=>'星期三'),
                array('name'=>'week_4','value'=>'星期四'),
                array('name'=>'week_5','value'=>'星期五'),
                array('name'=>'week_6','value'=>'星期六'),
            );
        }else{
            $item = array(
                array('name'=>'week_0','value'=>'星期日','checked'=>$info['week_0']? true : ''),
                array('name'=>'week_1','value'=>'星期一','checked'=>$info['week_1']? true : ''),
                array('name'=>'week_2','value'=>'星期二','checked'=>$info['week_2']? true : ''),
                array('name'=>'week_3','value'=>'星期三','checked'=>$info['week_3']? true : ''),
                array('name'=>'week_4','value'=>'星期四','checked'=>$info['week_4']? true : ''),
                array('name'=>'week_5','value'=>'星期五','checked'=>$info['week_5']? true : ''),
                array('name'=>'week_6','value'=>'星期六','checked'=>$info['week_6']? true : ''),
            );
        }
        $this->ajaxReturn($item);
    }

    public function edirMemberDay(){
        $shop_id = $_POST['id'];
        $value = $_POST['value'];
        $map = array();
        $map['wid'] = session('wid');
        $map['shop_id'] = $shop_id;
        $info = M('shop_card_yh')->where($map)->find();
        if(empty($info)){
            $data = array();
            $data['wid'] = session('wid');
            $data['shop_id'] = $shop_id;
            $data['week_0'] = (strpos($value,'week_0') === false) ? 0 : 1;
            $data['week_1'] = (strpos($value,'week_1') === false) ? 0 : 1;
            $data['week_2'] = (strpos($value,'week_2') === false) ? 0 : 1;
            $data['week_3'] = (strpos($value,'week_3') === false) ? 0 : 1;
            $data['week_4'] = (strpos($value,'week_4') === false) ? 0 : 1;
            $data['week_5'] = (strpos($value,'week_5') === false) ? 0 : 1;
            $data['week_6'] = (strpos($value,'week_6') === false) ? 0 : 1;
            $data['create_time'] = date('Y-m-d H:i:s',time());
            $data['status'] = 1;
            M('shop_card_yh')->add($data);
            $item = array(
                array('name'=>'week_0','value'=>'星期日','checked'=>(strpos($value,'week_0') === false)? '' : true),
                array('name'=>'week_1','value'=>'星期一','checked'=>(strpos($value,'week_1') === false)? '' : true),
                array('name'=>'week_2','value'=>'星期二','checked'=>(strpos($value,'week_2') === false)? '' : true),
                array('name'=>'week_3','value'=>'星期三','checked'=>(strpos($value,'week_3') === false)? '' : true),
                array('name'=>'week_4','value'=>'星期四','checked'=>(strpos($value,'week_4') === false)? '' : true),
                array('name'=>'week_5','value'=>'星期五','checked'=>(strpos($value,'week_5') === false)? '' : true),
                array('name'=>'week_6','value'=>'星期六','checked'=>(strpos($value,'week_6') === false)? '' : true),
            );
        }else{
            $data = array();
            $data['wid'] = session('wid');
            $data['shop_id'] = $shop_id;
            $data['week_0'] = (strpos($value,'week_0') === false) ? 0 : 1;
            $data['week_1'] = (strpos($value,'week_1') === false) ? 0 : 1;
            $data['week_2'] = (strpos($value,'week_2') === false) ? 0 : 1;
            $data['week_3'] = (strpos($value,'week_3') === false) ? 0 : 1;
            $data['week_4'] = (strpos($value,'week_4') === false) ? 0 : 1;
            $data['week_5'] = (strpos($value,'week_5') === false) ? 0 : 1;
            $data['week_6'] = (strpos($value,'week_6') === false) ? 0 : 1;
            M('shop_card_yh')->where($map)->save($data);
            $item = array(
                array('name'=>'week_0','value'=>'星期日','checked'=>(strpos($value,'week_0') === false)? '' : true),
                array('name'=>'week_1','value'=>'星期一','checked'=>(strpos($value,'week_1') === false)? '' : true),
                array('name'=>'week_2','value'=>'星期二','checked'=>(strpos($value,'week_2') === false)? '' : true),
                array('name'=>'week_3','value'=>'星期三','checked'=>(strpos($value,'week_3') === false)? '' : true),
                array('name'=>'week_4','value'=>'星期四','checked'=>(strpos($value,'week_4') === false)? '' : true),
                array('name'=>'week_5','value'=>'星期五','checked'=>(strpos($value,'week_5') === false)? '' : true),
                array('name'=>'week_6','value'=>'星期六','checked'=>(strpos($value,'week_6') === false)? '' : true),
            );
        }
        $this->ajaxReturn($item);
    }
    //店铺升级
    public function levelUp(){
        $map['wid'] = session('wid');
        $wechat = M("Wechat")->where($map)->find();
        $appid = $wechat['appid'];
        $mch_id = $wechat['mch_id'];
        $key = $wechat['partner_key'];

        //处理
        //$arr = ['高级店铺','汽车行业店铺','教育行业店铺','建材行业店铺'];
        $arr = ['gao','car','jiaoyu','jiancai','takeout', 'mini_program'];
        $idx = I('idx');
        $field = $arr[$idx];
        $money  = M('level_money')->where($map)->getField($field);

        $openid = I('openid');
        $map['openid'] = $openid;
        $map['is_pay'] = 1;
        $shop = M("Shop")->where($map)->find();
        //echo M()->getLastSql();exit;
        if(!$shop){
            $arr = array("err_code"=>400,"err_msg"=>"网络忙，请稍后再试~");
            $this->ajaxReturn($arr);
        }

        $log = M('level_order')->where(['openid'=>$openid,'wid'=>session('wid'),'is_pay'=>1,'status'=>0])->find();
        /*if($log){
            $arr = array("err_code"=>400,"err_msg"=>"您已提交申请，请耐心等待审核！");
            $this->ajaxReturn($arr);
        }*/
        $data['wid'] = $shop['wid'];
        $data['shop_id'] = $shop['shop_id'];
        $data['type'] = $field; //要升级店铺类型
        $data['openid'] = $openid;
        $data['out_trade_no'] = $this->build_order_no();
        $data['price'] = $money; //付款金额
        $data['status'] = 0;
        $data['is_pay'] = 0;
        $data['create_time'] = date("Y-m-d H:i:s",time());
        if(!$lv_orderid = M("level_order")->add($data)){
            $arr = array("err_code"=>400,"err_msg"=>"网络忙，请稍后再试！");
            $this->ajaxReturn($arr);
        }

        $url = "https://".$_SERVER['HTTP_HOST'].U("Notify/levelup");
        // echo $url;
        vendor('Weixinpay.WxPayJsApiPay');
        $tools = new \JsApiPay();
        $input = new \WxPayUnifiedOrder();
        if(!$data['remark']){
            $data['remark'] = "智联同城";
        }
        $input->SetBody($data['remark']);
        $input->SetAttach('店铺升级');
        $input->SetOut_trade_no($data['out_trade_no']);
        $input->SetTotal_fee(($data['price']*100));
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        // $input->SetGoods_tag("test3");
        $input->SetNotify_url($url);   //支付回调地址，这里改成你自己的回调地址。
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($_POST['openid']);
        $input->SetAppid($appid);
        $input->SetMch_id($mch_id);

        try{
            $wxorder = \WxPayApi::unifiedOrder($input, $key);
        }catch(Exception $e){
            $arr = array("err_code"=>400,"err_msg"=>$e->getMessage().$data['pay_money']);
            $this->ajaxReturn($arr);
        }
        if($wxorder['return_code'] == "FAIL"){
            $arr = array("err_code"=>400,"err_msg"=>$wxorder['return_msg'].$data['pay_money']);
            $this->ajaxReturn($arr);
        }
        try{
            $jsApiParameters = $tools->GetJsApiParameters($wxorder, $key);
        }catch(Exception $e){
            $arr = array("err_code"=>400,"err_msg"=>$e->getMessage());
            $this->ajaxReturn($arr);
        }
        //积分产生
        $pro = M('credit_setint')->where(['wid' => $shop['wid']])->find();
        $credit = $data['price'] * $pro['shop_lv'];
        addCredit($_POST['openid'], $credit, $lv_orderid, $shop['wid'], 3);

        echo $jsApiParameters;
    }
    public function article()
    {
        $map['type'] = I('type');
        $map['wid'] = session('wid');
        $list = M('level_article')->where($map)->order('id desc')->find();
        //echo M()->getLastSql();exit;
        $money = M('level_money')->where($map)->find();
        $money2['gao'] = $money['gao'];
        $money2['car'] = $money['car'];
        $money2['jiancai'] = $money['jiancai'];
        $money2['jiaoyu'] = $money['jiaoyu'];
        $money2['mini_program'] = $money['mini_program'];
        $arr['money'] = ['高级店铺','汽车行业店铺','教育行业店铺','建材行业店铺','餐饮行业店铺','独立小程序'];
        $arr['list'] = $list;
        $this->ajaxReturn($arr);
    }

    public function getVipRights () {
        $map['wid'] = session('wid');
        $map['state'] = 1;
        $list = M('agreement_rights')->where($map)->order('order_num desc')->select();
        $this->ajaxReturn($list);
    }

    public function getShopJionAgreement () {
        $map['wid']   = session('wid');
        $map['state'] = 1;
        $map['type']  = 3;
        $arr = M('agreement')->where($map)->find();
        $this->ajaxReturn($arr);
    }

    /*
     * 获取店员列表
     *
     * */
    public function getClerks () {
        //移除店员
        if (I('mid')) {
            $row = M('member')->where(['mid' => I('mid')])->save(['shopkeeper_openid' => 0]);
            $row ? $this->ajaxReturn(1) : $this->ajaxReturn(0);
        }
        //店主信息
        $openid = I('openid');
        $wid = I('wid');
        //店员列表
        $list = M('member')->where(['shopkeeper_openid' => $openid, 'wid' => $wid])->select();
        $this->ajaxReturn($list);
    }

    /*
     * 添加积分商品
     *
     * */
    public function addIntGoods () {
        //print_r($_POST);exit;
        $goods_id = $_POST['goods_id'];
        $post = $_POST;
        $post['shop_id'] = $post['store_id'];
        $post['wid'] = session('wid');
        $post['type_id'] = 0;
        $post['is_stop'] = $post['is_stop'] ? 1 : 0;
        $post['time'] = date('Y-m-d H:i:s', time());
        unset($post['img_url']);
        unset($post['store_id']);
        unset($post['goods_id']);
        if ($goods_id != 0) {
            $row =M('credit_goods')->where(['id' => $goods_id])->save($post);
        } else {
            $row =M('credit_goods')->add($post);
        }
        $row ? $this->ajaxReturn(['err_code' => 0, 'err_msg' => '操作成功']) : $this->ajaxReturn([['err_code' => 1, 'err_msg' => '网络忙']]);
    }

    /*
     * 获取积分商品
     *
     * */
    public function getIntGoods () {
        $info = M('shop')->where(['openid'=>I('openid'),'status'=>1,'is_pay'=>1,'join_end_time'=>['gt', date('Y-m-d H:i:s', time())]])->find();
        $list = M('credit_goods')->where(['shop_id' => $info['shop_id']])->order('order_num desc')->select();

        $this->ajaxReturn($list);
    }

    /*
     * 删除积分商品
     *
     * */
    public function delIntGood () {
        $re = M('credit_goods')->where(['id'=>I('id')])->delete();
        if($re){
            $arr = array("err_code"=>0, "err_msg"=>"删除成功！");
            $this->ajaxReturn($arr);
        }else{
            $arr = array("err_code"=>400,"err_msg"=>"网络忙，请稍后再试！");
            $this->ajaxReturn($arr);
        }
    }

    /*
     * 获取积分兑换
     *
     * */
    public  function getIntOrder () {
        $shop_id = I('shop_id');

        $list = M('credit_order')->alias('a')
            ->field('a.id as order_id, b.*, a.status')
            ->join('tp_credit_goods b ON a.goodsid = b.id')
            ->where(['a.wid' => session('wid'), 'b.shop_id' => $shop_id])
            ->order('a.time desc')
            ->select();
        $this->ajaxReturn($list);
    }

    /*
     * 通过积分兑换审核
     *
     * */
    function adoptIntOrder () {
        $id = I('orderid');
        $row = M('credit_order')->where(['id' => $id])->save(['status' => 2]);
        $row ? $this->ajaxReturn(['error' => 0, 'msg' => '已通过']) : $this->ajaxReturn(['error' => 1, 'msg' => '网络忙']);
    }
}